package com.xiaoma.myaudience.biz.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.activity.ActivitysDetailActivity;
import com.xiaoma.myaudience.biz.model.ActivityDetailInfoModel;
import com.xiaoma.myaudience.biz.model.RankingListModel;
import com.xiaoma.myaudience.biz.task.AddEventTask;
import com.xiaoma.myaudience.biz.view.LoadingImageView;
import com.xiaoma.myaudience.util.PicUtils;
import com.xiaoma.myaudience.util.model.ModelUtils;

/**
 * 排行榜
 * @author jpren
 *
 */
public class RankingListAdapter extends BaseAdapter{
	private AddEventTask mAddEventTask;

	private Context mContext;
	private List<Map<String, Object>> mList;	
	private LayoutInflater mInflater;
	private String mSizeStr;

	private String mErrorMsg;
	
	public RankingListAdapter(Context context, List<Map<String, Object>> list) {
		mContext = context;
		mList = list;
		mInflater = LayoutInflater.from(context);
		mSizeStr = PicUtils.composeSize((int) context.getResources().getDimension(
                R.dimen.list_item_img_width), (int) context.getResources().getDimension(
                R.dimen.list_item_img_height));
	}
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.ranking_list_item, null);
			ViewHolder holder = new ViewHolder();
			holder.img = (LoadingImageView) convertView.findViewById(R.id.img);
			holder.score = (TextView) convertView.findViewById(R.id.score);
			holder.ratingBar = (RatingBar) convertView.findViewById(R.id.rating_bar);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.zcr = (TextView) convertView.findViewById(R.id.zcr);
			holder.type = (TextView) convertView.findViewById(R.id.type);
			holder.jb = (TextView) convertView.findViewById(R.id.jb);
			holder.location = (TextView) convertView.findViewById(R.id.location);
			holder.startTime = (TextView) convertView.findViewById(R.id.start_time);
			holder.dateLine = (TextView) convertView.findViewById(R.id.dateline_time);
			holder.limitNum = (TextView) convertView.findViewById(R.id.limit_num);
			holder.verify = (TextView) convertView.findViewById(R.id.verify);
			holder.jifen = (TextView) convertView.findViewById(R.id.jifeng);
			holder.allTime = (TextView) convertView.findViewById(R.id.all_time);
			holder.play = (TextView) convertView.findViewById(R.id.play);
            convertView.setTag(holder);

		}
		ViewHolder holder = (ViewHolder) convertView.getTag();
		Map<String, Object> item = (Map<String, Object>) getItem(position);
		final String eventid = ModelUtils.getStringValue(item, ActivityDetailInfoModel.PARAM_EVENTID);
		holder.img.setLoadingDrawable(ModelUtils.getStringValue(item, RankingListModel.PARAM_POSTER), mSizeStr);
		String score = ModelUtils.getStringValue(item, RankingListModel.PARAM_STARLEVEL);
		Double d = Math.ceil(Double.valueOf(score)/2.0);
		holder.score.setText(score);
		holder.ratingBar.setRating(Float.valueOf(d.toString()));
		holder.title.setText(ModelUtils.getStringValue(item, RankingListModel.PARAM_TITLE));
		holder.zcr.setText("主持人：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_ZCR));
		holder.type.setText("活动类型：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_CLASSNAME));
		holder.jb.setText("活动嘉宾：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_JB));
		holder.location.setText("活动地点：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_LOCATION));
		holder.startTime.setText("开始时间：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_STARTTIME));
		holder.dateLine.setText("截止报名：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_DEADTIME));
		holder.limitNum.setText("活动人数：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_LIMITNUM));
		holder.verify.setText("需要审核：" + ("1".equals(ModelUtils.getStringValue(item, RankingListModel.PARAM_VERIFY)) ? "需要" : "不需要"));
		String jifen =  ModelUtils.getStringValue(item, RankingListModel.PARAM_JIFENG) ;
		int sjififen = Integer.valueOf(jifen);
		String jfString = (sjififen < 0 ? "参加活动：需要花费 " + (-sjififen) : "参加活动：奖励" + sjififen) + "能量/人";
		holder.jifen.setText(jfString);
		holder.allTime.setText(String.format("%s次查看 | %s人参加 | %s待审核", ModelUtils.getStringValue(item, RankingListModel.PARAM_VIEWNUM) ,
				ModelUtils.getStringValue(item, RankingListModel.PARAM_MEMBERNUM), 
				ModelUtils.getStringValue(item, RankingListModel.PARAM_SHENHE)));
		final String mStatus = ModelUtils.getStringValue(item, ActivityDetailInfoModel.PARAM_STATUS);
		getStatus(Integer.valueOf(mStatus), holder.play);
		
		holder.img.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, ActivitysDetailActivity.class);
				intent.putExtra(ActivityDetailInfoModel.PARAM_EVENTID, eventid);
				mContext.startActivity(intent);
			}
		});
		
//		holder.play.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				Boolean islogin = PrefHelper.getBoolean(mContext, Constant.LOGIN_ISLOGIN, false);
//		  		if (islogin) {
//		  			//已登录都给丫显示
//		  			showAddInfoDialog(1);
//		  			return;
//				} else {
//					showDialog(3);
//				}
//			}
//		});
		return convertView;
	}
	
	private void getStatus(int status, TextView btn) {
		String name = "参加活动";
		switch (status) {
		case 0:
			name = "参加活动";
			btn.setBackgroundResource(R.drawable.add_event_selector);
			break;
		case 1:
			name = "报名已截止";
			btn.setEnabled(false);
			btn.setBackgroundResource(R.drawable.rank_look_selector);
			break;
		case 2:
			name = "活动已结束";
			btn.setEnabled(false);
			btn.setBackgroundResource(R.drawable.rank_look_selector);
			
			break;
		case 3:
			name = "退出活动（待审核）";
			btn.setBackgroundResource(R.drawable.add_event_selector);
			break;
		case 4:
			name = "退出活动（已审核）";
			btn.setBackgroundResource(R.drawable.add_event_selector);
			break;

		default:
			break;
		}
		
		btn.setText(name);
	}

	private class ViewHolder {
        LoadingImageView img;
        TextView score;
        RatingBar ratingBar;
        TextView title;
        TextView zcr;
        TextView type;
        TextView jb;
        TextView location;
        TextView startTime;
        TextView dateLine;
        TextView limitNum;
        TextView verify;
        TextView jifen;
        TextView allTime;
        TextView play;
    }
}